package com.celesea.framework.util;

import com.celesea.framework.authority.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 环境工具类，用于获取环境参数，例如登录信息，品牌信息等
 */
public class EnvUtil {

    /**
     * 获取 HttpServletRequest
     */
    public static HttpServletResponse getResponse() {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getResponse();
        return response;
    }

    /**
     * HttpServletRequest
     *
     * @return request
     */
    public static HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        return request;
    }

    public static AuthorityPrincipal getAuthorityUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            return (AuthorityPrincipal) authentication.getPrincipal();
        } else {
            AuthorityPrincipal authorityUser = new AuthorityPrincipal();
            User user = new User();
            user.setId("1");
            user.setName("张一伟");
            authorityUser.setUser(user);
            Tenant tenant = new Tenant();
            tenant.setId("999");
            tenant.setName("XXX公司");
            authorityUser.setTenant(tenant);
            Dept dept = new Dept();
            dept.setId("1");
            dept.setName("XXX公司");
            authorityUser.setDept(dept);
            authorityUser.setJwtToken("eyJzdWIiOiJmNzg0MjIxYi1jYTUxLTQzOGUtYWM1Ny04MWQ5YmRmZmI4YmEiLCJpYXQiOjE1MTEzNTk1ODh9");
            return authorityUser;
        }
    }


    public static boolean isAuthority() {
        return SecurityContextHolder.getContext().getAuthentication() != null;
    }

    public static User getLoginUser() {
        return getAuthorityUser().getUser();
    }

    public static Dept getLoginDept() {
        return getAuthorityUser().getDept();
    }


    public static Tenant getLoginTenant() {
        return getAuthorityUser().getTenant();
    }


    public static String getTenantId() {
        return getAuthorityUser().getTenant().getId();
    }

    public static String getCompany() {
        return getAuthorityUser().getDept().getCompany();
    }

    public static boolean hasAnyRoles(String... roleCode) {
        List<String> roleCodes = Arrays.asList(roleCode);
        AuthorityPrincipal principal = getAuthorityUser();
        int matchCount = 0;
        if (CollectionKit.isNotEmpty(principal.getRoles())) {
            for (AuthorityRole role : principal.getRoles()) {
                //code or id
                if (roleCodes.contains(role.getCode())) {
                    matchCount++;
                }
            }
        }
        return matchCount > 0;
    }

    public static boolean hasAnyRoleIds(String... id) {
        List<String> ids = Arrays.asList(id);
        AuthorityPrincipal principal = getAuthorityUser();
        int matchCount = 0;
        if (CollectionKit.isNotEmpty(principal.getRoles())) {
            for (AuthorityRole role : principal.getRoles()) {
                //code or id
                if (ids.contains(role.getId())) {
                    matchCount++;
                }
            }
        }
        return matchCount > 0;
    }

    public static boolean hasAnyGroups(String groupId) {
        List<String> groupIds = Arrays.asList(groupId);
        AuthorityPrincipal principal = getAuthorityUser();
        int matchCount = 0;
        if (CollectionKit.isNotEmpty(principal.getGroups())) {
            for (AuthorityGroup item : principal.getGroups()) {
                if (groupIds.contains(item.getId())) {
                    matchCount++;
                }
            }
        }
        return matchCount > 0;
    }

}
